package com.huizhonglm.diap.ui.wallet;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bruce.common.utils.EmptyUtil;
import com.bruce.pullrefresh.PullToRefreshBase;
import com.bruce.pullrefresh.PullToRefreshListView;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.wallet.PointInfo;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.presenters.wallet.MyPointPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.wallet.iView.MyPointIView;
import com.huizhonglm.diap.widget.LoadView;

import java.util.List;

/**
 * 我的积分数据
 */
public class MyPointActivity extends BaseActivity implements MyPointIView,
        AdapterView.OnItemClickListener, LoadView.OnLoadAgainListener,
        PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemLongClickListener {

    private PullToRefreshListView pullView;
    private ListView mListView;
    private LoadView loadView;

    private List<PointInfo> mData;
    private MyPointAdapter mAdapter;
    private MyPointPresenter mPresenter;

    private String userID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_point);
        setBackEvent(this, null);
        userID = UserUtil.getUserId();
        mPresenter = new MyPointPresenter(this);
        assignViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        onLoadAgain();
    }

    @Override
    protected void assignViews() {
        loadView = (LoadView) findViewById(R.id.load_view);
        pullView = (PullToRefreshListView) findViewById(R.id.my_collection_list);
        pullView.setMode(PullToRefreshBase.Mode.BOTH);
        pullView.setOnRefreshListener(this);
        mListView = pullView.getRefreshableView();
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);
        loadView.setDataView(pullView, this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        return true;
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        mPresenter.loadData(Constants.LOAD_DROP, userID);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        mPresenter.loadData(Constants.LOAD_MORE, userID);
    }

    @Override
    public void onLoadAgain() {
        loadView.showProgress();
        mPresenter.loadData(Constants.LOAD_INIT, userID);
    }

    @Override
    public void onLoadDataSuccess(String pullFlag, List<PointInfo> data) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                this.mData = data;
                updatePage(data);
                loadView.showDataView();
                break;
            case Constants.LOAD_DROP:
                this.mData = data;
                updatePage(data);
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                this.mData.addAll(data);
                updatePage(data);
                pullView.onRefreshComplete();
                break;
        }
    }

    @Override
    public void onLoadDataFailure(String pullFlag) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                loadView.showErrorView();
                break;
            case Constants.LOAD_DROP:
                ToastUtil.makeTextShort("刷新失败");
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                ToastUtil.makeTextShort("加载更多失败");
                pullView.onRefreshComplete();
                break;
        }
    }

    @Override
    public void onLoadDataEmpty(String pullFlag) {
        switch (pullFlag) {
            case Constants.LOAD_INIT:
                loadView.showEmptyView(null);
                break;
            case Constants.LOAD_DROP:
                ToastUtil.makeTextShort("列表已经是最新");
                pullView.onRefreshComplete();
                break;
            case Constants.LOAD_MORE:
                ToastUtil.makeTextShort("到底部啦");
                pullView.onRefreshComplete();
                break;
        }
    }

    private void updatePage(List<PointInfo> data){
        setPullViewMode(data);
        if (mAdapter == null) {
            mAdapter = new MyPointAdapter(getLayoutInflater(), mData);
            mListView.setAdapter(mAdapter);
        } else {
            mAdapter.setData(mData);
            mAdapter.notifyDataSetChanged();
        }
    }

    private void setPullViewMode(List<PointInfo> data){
        if (!EmptyUtil.isEmpty(data)) {
            if (mData.size() >= Constants.PAGE_SIZE) {
                if (data.size() < Constants.PAGE_SIZE) {
                    pullView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                } else {
                    pullView.setMode(PullToRefreshBase.Mode.BOTH);
                }
            } else {
                pullView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
        }
    }
}
